﻿using System;
using Framework.MVC;
using Identity.Model;
using Account.Model;
using Account.Model.Behavior;
using Framework.MVVM.Model;
using Identity.IService;
using Identity.Service;
using Account.IService;
using Account.Service;
using Common.Service;
using Common.IService;


namespace FrontCenter.Models
{
    [ServiceAttribute(typeof(IAnswerService), typeof(AnswerService), "Instance")]
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class ValidateAnswerModel : AsyncModelBase, IValidateIdentity, IValidateAnswer, INewIdentity,IDeleteIdentity
    {
        #region Field

        private const string m_resource_user = "getpassword";

        private const string m_user = "user";
        private const string m_code = "code";
        private const string m_error = "error";

        #endregion Field

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            this.OnBeginRequest();
            this.ValidateIdentity(m_resource_user, string.Empty, string.Empty, string.Empty);
        }

        #endregion AsyncModelBase

        #region IValidateIdentity

        public string IdentityCode { get; set; }

        public bool IsOnce { get; set; }

        public string Source { get; set; }

        public void OnValidateIdentityComplete(string key, Identity.IService.Entry.IdentityEntry entry)
        {
            AccountCode = entry.value;
            IdentityValue = entry.value;
            IdentityResource = "modifypassword";
            this.ValidateAnswer(m_user, string.Empty, string.Empty);
        }

        public void OnValidateIdentityError(string key)
        {
            OnComplete(m_error, "identity");
            this.OnEndRequest();
        }

        #endregion IValidateIdentity

        #region IValidateAnswer

        public string AccountCode { get; set; }

        public string Answer { get; set; }

        public void OnValidateAnswerComplete(string key, bool entry)
        {
            IdentityResource = "modifypassword";
            this.NewIdentity(string.Empty, string.Empty);
        }

        public void OnValidateAnswerError(string key)
        {
            OnComplete(m_error,"answer");
            this.OnEndRequest();
        }

        public string QuestionCode { get; set; }

        #endregion IValidateAnswer

        #region INewIdentity

        public string IdentityResource { get; set; }

        public string IdentityValue { get; set; }

        public void OnNewIdentityComplete(string key, Identity.IService.Entry.IdentityEntry entry)
        {
            OnComplete(m_code, entry.code);
            IdentityResource = "getpassword";
            this.DeleteIdentity(string.Empty, string.Empty);
        }

        public void OnNewIdentityError(string key)
        {
            OnComplete(m_error, "newidentity");
            this.OnEndRequest();
        }

        #endregion INewIdentity

        #region IDeleteIdentity

        public void OnDeleteIdentityComplete(string key, Identity.IService.Entry.IdentityEntry entry)
        {
            OnEndRequest();
        }

        public void OnDeleteIdentityError(string key)
        {
            OnEndRequest();
        }

        #endregion IDeleteIdentity
    }
}